<?php

namespace App\Transformers;

use App\Models\Post;
use League\Fractal\TransformerAbstract;

/**
 * Class PostTransformer
 * @package namespace App\Transformers;
 */
class PostTransformer extends TransformerAbstract
{

    protected $availableIncludes = [];
    protected $defaultIncludes = ['category', 'tags', 'comments'];

    /**
     * Transform the \Post entity
     * @param Post $model
     *
     * @return array
     */
    public function transform(Post $model)
    {
        return [
            'id' => (int)$model->id,
            /* place your other model properties here */

            'title' => $model->title,
            'summary' => $model->summary,
            'content' => $model->content,
            'category' => $model->category,

            'created_at' => $model->created_at,
            'updated_at' => $model->updated_at
        ];
    }


    protected function includeCategory(Post $post)
    {
        $category = $post->category;
        return $this->item($category, new CategoryTransformer());
    }

    protected function includeTags(Post $post)
    {
        $tags = $post->tags;
        return $this->collection($tags, new TagTransformer());
    }

    protected function includeComment(Post $post)
    {
        $comments = $post->comments;
        return $this->collection($comments, new CommentTransformer());
    }
}
